package com.utils.basehelper.money;

import org.apache.commons.lang.StringUtils;

import java.text.DecimalFormat;

/**
 * @author: zhaozongyi
 * @date: 2023-09-04 18:45
 */
public class ConverUtil {

    public static void main(String[] args) {
        System.out.println(convertUpperCN(90000L));
        System.out.println(formatRMB(1003023L));
    }

    public static final String RMB = "￥";
    public static final String RMB_FORMAT = "###,##0.00";

    /**
     * 将数字转换成￥格式，单位元
     *
     * @param amount 以分为单位的金额
     * @return
     */
    public static String formatRMB(Long amount) {
        String src = com.zr.test.MoneyConvertUtil.convertToYuan(amount);
        if (StringUtils.isBlank(src)) {
            return StringUtils.EMPTY;
        }
        DecimalFormat df = new DecimalFormat(RMB_FORMAT);
        return RMB + df.format(Double.valueOf(src));
    }

    public static String convertUpperCN(Long money) {
        if (money != null && money >= 1L) {
            String[] CN_UPPER_NUMBER = new String[]{"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
            String[] CN_UPPER_MONETRAY_UNIT = new String[]{"分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "兆", "拾", "佰", "仟"};
            String s = money.toString();
            StringBuilder stringBuilder = new StringBuilder();
            int i = 0;

            int size;
            for(size = s.length(); i < size; ++i) {
                char c = s.charAt(i);
                Integer num = Integer.valueOf(String.valueOf(c));
                stringBuilder.append(CN_UPPER_NUMBER[num]);
                stringBuilder.append(CN_UPPER_MONETRAY_UNIT[size - i - 1]);
            }

            String[][] var9 = new String[][]{{"零分", "零"}, {"零角", "零"}, {"零拾", "零"}, {"零佰", "零"}, {"零仟", "零"}, {"零万", "万"}, {"零亿", "亿"}, {"零零", "零"}, {"零元", "元"}};
            size = var9.length;

            for(int var10 = 0; var10 < size; ++var10) {
                String[] replaces = var9[var10];
                replaceUpperCN(stringBuilder, replaces[0], replaces[1]);
            }

            while(stringBuilder.charAt(stringBuilder.length() - 1) == '零') {
                stringBuilder.deleteCharAt(stringBuilder.length() - 1);
            }

            if (stringBuilder.charAt(stringBuilder.length() - 1) == 20803) {
                stringBuilder.append("整");
            }

            return stringBuilder.toString();
        } else {
            return "";
        }
    }

    private static void replaceUpperCN(StringBuilder stringBuilder, String replace, String replaceVal) {
        String s;
        for(s = stringBuilder.toString(); s.indexOf(replace) != -1; s = s.replace(replace, replaceVal)) {
        }

        stringBuilder.setLength(0);
        stringBuilder.append(s);
    }
}
